#include "ThreadPool.h"
void* threadFunc(void* arg){
    threadPool_t* pthreadPool=(threadPool_t*)arg;
    while(1){
        pthread_mutex_lock(&pthreadPool->mutex);
        while(pthreadPool->taskQueue.queueSize==0 && pthreadPool->exitflag==0){
            pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);
        }
        if(pthreadPool->exitflag==1){
            pthread_mutex_unlock(&pthreadPool->mutex);
            pthread_exit(NULL);
        }
        printf("子线程获取一个任务\n");
        int netfd=pthreadPool->taskQueue.front->netfd;
        dequeue(&pthreadPool->taskQueue);
        pthread_mutex_unlock(&pthreadPool->mutex);
        //做任务
        //初始化数据库，链接数据库
        MYSQL* mysql = mysql_init(NULL);
        MYSQL* Mret = mysql_real_connect(mysql, "localhost", "root", "ch200319",
                                       "client", 0, NULL, 0);
        //验证
        MYSQL_CONNECT_CHECK(Mret, mysql);
        char name[128] = {0};
        int ret = loginFunc(netfd, name, mysql);
        LOG_CHECK("login", name);
        //
        printf("%s 登录成功\n",name);
        // 所有用户初始目录都为他们的根目录
        int curr_folder = getCurFolder(name, mysql);
        printf("curr_folder = %d\n", curr_folder);

        train_t working_dir;
        char msg[4096] = {0};

        while(1){
            // 接收客户端的命令和参数
            int operat = 0;
            int arg_length;

            // 接收命令
            ret = recvn(netfd, &operat, sizeof(int));
            if(ret == 0){
                break;
            }
            printf("operate = %d\n", operat);

            // 接收参数
            recvn(netfd, &arg_length, sizeof(int));
            char* arg = (char*)malloc(arg_length + 1);
           
            recvn(netfd, arg, arg_length);
            arg[arg_length] = '\0';
            printf("参数：%s\n",arg);

           // if(arg[0] != '\0'){
           //     arg[arg_length - 1] = '\0';
           // }

            switch(operat){
            case 0:
                ret = cdFunc(name, arg, &curr_folder, mysql);
                LOG_CHECK("cd", arg);
               // error_send(ret, netfd);
                send(netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
                break;

            case 1:
                bzero(msg, sizeof(msg));
                ret = lsFunc(msg, name, arg, curr_folder, mysql);
                LOG_CHECK("ls", arg);
                send(netfd,&ret,sizeof(ret),MSG_NOSIGNAL);
                int length_ls=strlen(msg);
                send(netfd,&length_ls,sizeof(int),MSG_NOSIGNAL);
                send(netfd,msg,length_ls,MSG_NOSIGNAL);
                break;

            case 2:
                ret = putsTextFunc(name, arg, netfd, mysql, curr_folder);
                LOG_CHECK("puts", arg);
                break;

            case 3:
                printf("getsfunc: name=%s,arg=%s\n",name,arg);
                ret = getsFunc(name, arg, curr_folder, netfd, mysql);
                LOG_CHECK("gets", arg);
                break;

            case 4:
                ret = removeFunc(curr_folder, arg, name, mysql);
                LOG_CHECK("remove", arg);
                //error_send(ret, netfd);
                send(netfd,&ret,sizeof(int),MSG_NOSIGNAL);
                break;

            case 5:
                bzero(msg, sizeof(msg));
                ret = pwd(msg, curr_folder, mysql);
                LOG_CHECK("pwd", arg);
                //func_send(ret, netfd, msg);
                send(netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
                int lengthofmsg=strlen(msg);
                send(netfd,&lengthofmsg,sizeof(int),MSG_NOSIGNAL);
                send(netfd,msg,lengthofmsg,MSG_NOSIGNAL);//发送工作目录
                break;

            case 6:
                ret = mkdirFunc(curr_folder, arg, name, mysql);
                LOG_CHECK("mkdir", arg);
                send(netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
                //error_send(ret, netfd);
                break;

            case 7:
                ret = rmdirFunc(curr_folder, arg, name, mysql);
                LOG_CHECK("rmdir", arg);
                //error_send(ret, netfd);
                send(netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
                break;

            default:
                //send(netfd, &ret, sizeof(int), MSG_NOSIGNAL);
                printf("客户发来未知命令，不予响应\n");//反应客户需求的记录日志
            }
        }
        mysql_close(mysql);
        close(netfd);
    }
}
//创建线程
int makeWorker(threadPool_t * pthreadPool){

    for(int i=0;i<pthreadPool->workerNum;i++){

       int ret= pthread_create(&pthreadPool->tidArry[i],NULL,threadFunc,pthreadPool);
        if(ret!=0){
            error(1,errno,"pthread create is error\n");
        }
    }
    return 0;
}
